package cn.wjj.org.controller;

import cn.wjj.basic.util.AjaxResult;
import cn.wjj.basic.util.PageList;
import cn.wjj.org.domain.Department;
import cn.wjj.org.query.DepartmentQuery;
import cn.wjj.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 查询所有对象接口
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有",notes = "")
    public List<Department> findAll(){
        return departmentService.queryAll();
    }

    /**
     * 根据di查询接口
     * @param id
     * @return
     */
    @GetMapping("{id}")
    @ApiOperation(value = "查询单个id",notes = "需要传id")
    public Department findById(@PathVariable("id")Long id){
        return departmentService.queryByid(id);
    }

    /**
     * 分页查询接口
     * @param query
     * @return
     */
    @PostMapping()
    @ApiOperation(value = "分页查询接口",notes = "需要传分页数据")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
        return departmentService.queryPage(query);
    }

    /**
     * 删除接口
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除",notes = "需要传id")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            departmentService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("删除失败"+e.getMessage());
        }
    }

    /**
     * 添加或修改接口
     * @param department
     * @return
     */
    @PutMapping()
    @ApiOperation(value = "添加或者修改",notes = "添加不需要id  修改需要传id")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if (department.getId()==null){
                departmentService.add(department);
            }else {
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("操作失败"+e.getMessage());
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除接口",notes = "需要传要删除的id")
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            departmentService.batchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("批量删除失败!"+e.getMessage());
        }
    }

    /**
     *  加载部门树
     * @return
     */
    @GetMapping("/deptTree")
    @ApiOperation(value = "加载部门树接口" ,notes = "")
    public List<Department> gerDeptTree(){
        return departmentService.gerDeptTree();
    }
}
